﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Resources;

namespace SolitaireHTML5
{
    public partial class MainPage : PhoneApplicationPage
    {
        private BrowserMouseHelper moveHelper;
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            webBrowser.Opacity = 0;

            moveHelper = new BrowserMouseHelper(ref webBrowser);
            moveHelper.ScrollDisabled = true;

            SaveFilesToIsoStore();

            webBrowser.Navigate(new Uri("html_content/index.html", UriKind.Relative));
        }


        private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            // Show browser
            webBrowser.Opacity = 1;

        }


        private void SaveFilesToIsoStore()
        {
            //These files must match what is included in the application package,
            //or BinaryStream.Dispose below will throw an exception.
            string[] files = {
            "html_content/index.html",
            "html_content/index2.html",
            "html_content/script.js",
            "html_content/wp.css",
            "html_content/gfx/background.png",
            "html_content/gfx/card_background.png",
            "html_content/gfx/deck_background.png",
            "html_content/gfx/Club_ace.png",
            "html_content/gfx/Club_2.png",
            "html_content/gfx/Club_3.png",
            "html_content/gfx/Club_4.png",
            "html_content/gfx/Club_5.png",
            "html_content/gfx/Club_6.png",
            "html_content/gfx/Club_7.png",
            "html_content/gfx/Club_8.png",
            "html_content/gfx/Club_9.png",
            "html_content/gfx/Club_10.png",
            "html_content/gfx/Club_jack.png",
            "html_content/gfx/Club_queen.png",
            "html_content/gfx/Club_king.png",
            "html_content/gfx/Diamond_ace.png",
            "html_content/gfx/Diamond_2.png",
            "html_content/gfx/Diamond_3.png",
            "html_content/gfx/Diamond_4.png",
            "html_content/gfx/Diamond_5.png",
            "html_content/gfx/Diamond_6.png",
            "html_content/gfx/Diamond_7.png",
            "html_content/gfx/Diamond_8.png",
            "html_content/gfx/Diamond_9.png",
            "html_content/gfx/Diamond_10.png",
            "html_content/gfx/Diamond_jack.png",
            "html_content/gfx/Diamond_queen.png",
            "html_content/gfx/Diamond_king.png",
            "html_content/gfx/Heart_ace.png",
            "html_content/gfx/Heart_2.png",
            "html_content/gfx/Heart_3.png",
            "html_content/gfx/Heart_4.png",
            "html_content/gfx/Heart_5.png",
            "html_content/gfx/Heart_6.png",
            "html_content/gfx/Heart_7.png",
            "html_content/gfx/Heart_8.png",
            "html_content/gfx/Heart_9.png",
            "html_content/gfx/Heart_10.png",
            "html_content/gfx/Heart_jack.png",
            "html_content/gfx/Heart_queen.png",
            "html_content/gfx/Heart_king.png",
            "html_content/gfx/Spade_ace.png",
            "html_content/gfx/Spade_2.png",
            "html_content/gfx/Spade_3.png",
            "html_content/gfx/Spade_4.png",
            "html_content/gfx/Spade_5.png",
            "html_content/gfx/Spade_6.png",
            "html_content/gfx/Spade_7.png",
            "html_content/gfx/Spade_8.png",
            "html_content/gfx/Spade_9.png",
            "html_content/gfx/Spade_10.png",
            "html_content/gfx/Spade_jack.png",
            "html_content/gfx/Spade_queen.png",
            "html_content/gfx/Spade_king.png"
            };

            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            //if (false == isoStore.FileExists(files[0]))
            //{
                foreach (string f in files)
                {
                    StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
                    using (BinaryReader br = new BinaryReader(sr.Stream))
                    {
                        byte[] data = br.ReadBytes((int)sr.Stream.Length);
                        SaveToIsoStore(f, data);
                    }
                }
            //}
        }

        private void SaveToIsoStore(string fileName, byte[] data)
        {
            string strBaseDir = string.Empty;
            string delimStr = "/";
            char[] delimiter = delimStr.ToCharArray();
            string[] dirsPath = fileName.Split(delimiter);

            //Get the IsoStore.
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            //Re-create the directory structure.
            for (int i = 0; i < dirsPath.Length - 1; i++)
            {
                strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
                isoStore.CreateDirectory(strBaseDir);
            }

            //Remove the existing file.
            if (isoStore.FileExists(fileName))
            {
                isoStore.DeleteFile(fileName);
            }

            //Write the file.
            using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
            {
                bw.Write(data);
                bw.Close();
            }
        }

    }




    
}